/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ContentManagers.Servlets;

import ContentManagers.Models.Product;
import ContentManagers.RoleManagers.PMManager;
import ContentManagers.Security.Validator;
import ContentManagers.Security.WebLogger;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Ervin James
 */
public class ServletAddProduct extends HttpServlet {

    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
         HttpSession session = request.getSession();
        try {
            boolean check,exist;          
            PMManager pm = new PMManager();
            Product newProduct = new Product();
            String name = Validator.inputSanitizer(request.getParameter("name"));
            String description = Validator.inputSanitizer(request.getParameter("description"));
            String sPrice = request.getParameter("price");
            String username= session.getAttribute("username").toString();
            double fPrice=0;           

            //validation part
            if(name.length()>45 || name.isEmpty()) {
                WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                session.setAttribute("message", "1");
                response.sendRedirect("./Managing Interface/managing_index.jsp");
            }    
                       
            else if(description.length()>500 || description.isEmpty()) {
                WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                session.setAttribute("message", "2");
                response.sendRedirect("./Managing Interface/managing_index.jsp");
            }
            
            //ERROR HERE 
            else if(sPrice.isEmpty()||sPrice.length()>6)
            {
                WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                session.setAttribute("message", "3");
                response.sendRedirect("./Managing Interface/managing_index.jsp");
            }
            
            else 
            {
                 fPrice = Double.parseDouble(sPrice);
                 
                if(fPrice <= 0 || fPrice>999999)
                {
                    WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                    session.setAttribute("message", "3");
                    response.sendRedirect("./Managing Interface/managing_index.jsp");
                }
                else 
                {
                    //set values for model
                    newProduct.setName(name);
                    newProduct.setDescription(description);
                    newProduct.setPrice(fPrice);
                    newProduct.setPtid(Integer.parseInt(session.getAttribute("managerType").toString()));

                    exist = pm.checkProduct(newProduct); 
                    if(exist==true)
                    {
                        WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                        session.setAttribute("message", "5");
                        response.sendRedirect("./Managing Interface/managing_index.jsp");
                    }
                    else
                    {
                        check = pm.addProduct(newProduct);
                         
                        if(check==true)
                        {
                            WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Successfully added product "+name+". Manager: " + session.getAttribute("username"),true);
                            session.setAttribute("message", "6");
                            response.sendRedirect("./Managing Interface/managing_index.jsp");
                        }
                        else
                        {
                            WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Add product failed. Manager: " + session.getAttribute("username"),false);
                            session.setAttribute("message", "4");
                            response.sendRedirect("./Managing Interface/managing_index.jsp");              
                        }
                    }
                }
            }     
        }
        catch(NumberFormatException e) {
                WebLogger.insertLog(request.getRemoteAddr().toString(),request.getHeader("referer"),request.getRequestURI().toString(),"Number format exception.",false);
                session.setAttribute("message", "3");
                response.sendRedirect("./Managing Interface/managing_index.jsp");
            }

        finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
